package com.aiju.cash.ui.multiple_shops.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.bean.RelationGoodManager;

/**
 * @author: 佐为
 * @date: 2017-06-15 19:33
 * @version: V1.0
 * @Description: 关联商品分类适配器
 */
public class GoodCategoryAdapter extends RecyclerView.Adapter<GoodCategoryAdapter.ViewHolder> {

    private Context context;

    private RelationGoodManager manager;

    public GoodCategoryAdapter(Context context, RelationGoodManager manager) {
        this.context = context;
        this.manager = manager;
    }

    @Override
    public GoodCategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_relation_category, null));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        GoodsType goodsType = manager.getGoodsTypes().get(position);
        holder.rootView.setSelected(goodsType.isSeleted());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
                v.setSelected(true);
            }
        });
        holder.nameTextView.setText(goodsType.getName());
        int sum = manager.getGoodTypeForRelationSum(goodsType.getIdInt());
        if (sum > 0) {
            Spanned spanned = Html.fromHtml("<font color='#1e82d2'>" + sum + "</font>/" + goodsType.getGoodsList().size());
            holder.textViewTip.setText(spanned);
        } else {
            holder.textViewTip.setText(sum + "/" + goodsType.getGoodsList().size());
        }
    }

    @Override
    public int getItemCount() {
        return manager.getGoodsTypes().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private View rootView;
        private TextView nameTextView;
        private TextView textViewTip;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            nameTextView = (TextView) itemView.findViewById(R.id.textview_categoryname);
            textViewTip = (TextView) itemView.findViewById(R.id.tv_tip);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
